﻿using System;
using System.Collections.Generic;
using System.Text;
using Trends.SAGRES;
using DaCredito;
using System.Data.SqlClient;
using Trends.TFC.Data;

namespace Trends.SAGRES.NHibernate
{
    public class SagresIdGenerator 
    {
        #region IIdentifierGenerator Members

        private string isPrefixo;
        private Sistemas ieSistema;

        public object Generate(global::NHibernate.Engine.ISessionImplementor session, object obj)
        {
            GerarCodigo loGerarCodigo = new GerarCodigo(new Connection(session.Connection));
            loGerarCodigo.Sistema = this.ieSistema;
            loGerarCodigo.PrefixoTabela = this.isPrefixo;
            loGerarCodigo.TipoGeracaoCodigo = TiposGeracaoCodigo.Surrogate;
            decimal ldcId = (decimal)loGerarCodigo.Gerar();
            return ldcId;
        }

        #endregion

        #region IConfigurable Members

        public void Configure(global::NHibernate.Type.IType type, System.Collections.IDictionary parms, global::NHibernate.Dialect.Dialect d)
        {
            this.isPrefixo = (string)parms["prefixo"];
            if (String.IsNullOrEmpty(this.isPrefixo))
            {
                string lsTable = (string)parms["target_table"];
                this.isPrefixo = lsTable.Substring(0, 3);
            }
            string lsSistema = (string)parms["sistema"];
            this.ieSistema = (Sistemas)Enum.Parse(typeof(Sistemas), lsSistema, true);
        }

        #endregion
    }
}
